package com.rk.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rk.admin.aop.SystemControllerLog;
import com.rk.admin.domain.Comarticle;
import com.rk.admin.domain.Particle;
import com.rk.admin.mapper.ParticleMapper;
import com.rk.admin.service.ParticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParticleServiceImpl implements ParticleService {

    @Autowired
    private ParticleMapper particleMapper;


    /**
     * 添加本科生文章
     * @param particle
     * @return
     */
    @Override
    @SystemControllerLog(description ="添加人才培养本科生文章")
    public Boolean addben(Particle particle) {
        return particleMapper.insertParticleben(particle)>0;
    }

    /**
     * 添加研究生文章
     *
     * @param particle
     * @return
     */
    @Override
    @SystemControllerLog(description ="添加人才培养研究生文章")
    public Boolean addyan(Particle particle) {
        return particleMapper.insertParticleyan(particle)>0;
    }


    /**
     * 条件分页查询
     *
     * @param page
     * @param limit
     * @param title
     * @return
     */
    @Override
    public IPage<Particle> selectPage(int page, int limit, String title,int model) {
        IPage<Particle> pageinfo=new Page<>(page,limit);
        //条件查询
        LambdaQueryWrapper<Particle> lqw=new LambdaQueryWrapper<Particle>();
        //条件:1、本科生/研究生      2、文章创建时间降序
        lqw.eq(Particle::getModel,model).orderByDesc(Particle::getCreatime);
        //指定查询字段
        lqw.select(Particle::getId,Particle::getTitle,Particle::getCreatime,Particle::getModel);

        //根据文章标题进行条件查询
        if(!"".equals(title)&&title!=null){
            lqw.like(Particle::getTitle,title);
        }
        particleMapper.selectPage(pageinfo,lqw);
        return pageinfo;
    }


    /**
     * 通过id删除文章
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Integer id) {
        return particleMapper.deleteById(id)>0;
    }

    /**
     * 通过id修改
     * @param particle
     * @return
     */
    @Override
    public Boolean updatebyId(Particle particle) {
        return particleMapper.updatebyid(particle)>0;
    }

    /**
     * 通过id查找
     *
     * @param id
     * @return
     */
    @Override
    public Particle findbyId(Integer id) {
        return particleMapper.queryById(id);
    }

    /**
     * 查询最后count数据
     *
     * @param count
     * @return
     */
    @Override
    public List<Particle> findcount(int count) {
        Integer total = particleMapper.selectCount(null);
        System.out.println("总数为:"+total);
        if(count>total){//如果需要的数量大于表中已有的数据  就返回所有数据
            List<Particle> particles = particleMapper.selectList(null);
            System.out.println(particles.toString());
            return particles;
        }
        return particleMapper.findcount(count);
    }


}
